﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.SYS.Library;
using ZX.CRM.Bussiness;
using ZX.CRM.Library;
using ZX.DBModule;
using ZX.Common;
using ZX.BLL.Extend;
using ZX.BLL;
using ZX.Mapping;
using ZX.Web.UI.Controls.ControlBind;
using System.Data;
using ZX.CRM.Web.CrmWorkFlowService;

namespace ZX.WebApp.Crm.Deliver
{
    public partial class AddDeliverTrain : EditUIPage
    {
        BTCrmCrustomerOrder order = new BTCrmCrustomerOrder();
        BTCrmCustomer bll = new BTCrmCustomer();
        BDatPhraseDetail bllDetail = new BDatPhraseDetail();
        BCrmDeliverMajor bllDeliverMajorProject = new BCrmDeliverMajor();
        string strDeliverTrainInfo = "[]", wfid = "", orderProduct = "", wtid = "", strTeacher = "", orderId = "", productId = "";
        WorkFlowServiceClient client = new WorkFlowServiceClient();
        /// <summary>
        /// 退回标志
        /// </summary>
        protected string strBack { get; set; }
        public string workflowInsId
        {
            get;
            set;
        }
        BCrmDeliverTrain OrderProduct = new BCrmDeliverTrain();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["WorkflowInsId"] != null)
            {
                workflowInsId = Request["WorkflowInsId"].ToString();
                Inits(workflowInsId);
            }
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string ids = this.GetStringFromQueryString("id", "");
            BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            BaseEntity entity = bInstance.GetEntityById(ids) as BaseEntity;
            this.EditTemplate.Text = CreateInstanceJsObject(instance, entity);
            //交付单状态
            IList<DatPhraseDetail> ListDliverId = bllDetail.GetDatPhraseDetailByClassValue("DeliverState");
            DropDownListControl.DropDataSourceBind(this.DliverState, ListDliverId, "PhraseText", "PhraseID");
            if (Flag == "0")
            {
                this.Code.Text = IDMaker.GetSequenceStr("TRAIN001");
                this.DeliverDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.ID.Value = Guid.NewGuid().ToString();
                DliverState.SelectedValue = "8A41F2EF-989F-4871-BBE9-91CDFF4235CA";
                this.txtXm.Value = this.User.UName;
                this.WorkFlowInsId.Value = Guid.NewGuid().ToString();
                this.WorkTaskInsId.Value = Guid.NewGuid().ToString();
            }
            if (Flag == "1")
            {
                string strId = this.GetStringFromQueryString("id", "");
                if (strId == "")
                {
                    strId = this.GetStringFromQueryString("WorkflowInsId", "");
                }
                IList<V_CrmDeliverTrain> instanceMajor = bllDeliverMajorProject.GetAllDeliverTrainInfo(new Guid(strId));
                if (instanceMajor != null && instanceMajor.Count > 0)
                {
                    if (strDescript == "nxja" || strDescript == "market")
                    {
                        btnMemember.Visible = false;
                        DliverState.SelectedValue = "20F825C4-292A-4386-8157-923668BD2682";
                    }
                    btnOrderProductID.Visible = false;
                    btnCustomerMgr.Visible = false;
                    btnCustomerDirector.Visible = false;
                    btnCustomerManager.Visible = false;

                    btnOrderProductID.Visible = false;
                    if (strView != "unDone")
                    {
                        btnClassFrontServer.Visible = false;
                    }
                    orderId = instanceMajor[0].OrderID.ToString();
                    productId = instanceMajor[0].ProductId.ToString();
                    orderProduct = instanceMajor[0].OrderProductID.ToString();
                    this.ID.Value = instanceMajor[0].ID.ToString();
                    this.Code.Text = instanceMajor[0].Code;
                    this.DeliverDate.Value = instanceMajor[0].AddTime.ToString("yyyy-MM-dd") == "0001-01-01" ? strView == "check" ? "" : DateTime.Now.ToString("yyyy-MM-dd") : instanceMajor[0].AddTime.ToString("yyyy-MM-dd");
                    this.txtOrderProductID.Value = instanceMajor[0].OrderCode;
                    this.CustomerCode.Value = instanceMajor[0].Code;
                    this.CustomerName.Value = instanceMajor[0].CustomerName;
                    //this.Pay1.Value = instanceMajor[0].AccountReceivable.ToString();
                    //this.Pay2.Value = instanceMajor[0].ResignMoney.ToString();
                    this.OrderTypeName.Value = instanceMajor[0].CustomerOrigin;
                    this.CompanyProduct.Value = instanceMajor[0].CompanyProduct;
                    this.TypeName.Value = instanceMajor[0].CustomerType;
                    this.AddUserName.Value = instanceMajor[0].CInfoer;
                    this.SaleaddName.Value = instanceMajor[0].CSaler;
                    this.LectorName.Value = instanceMajor[0].CTeacher;

                    this.CustomerMgr.Value = instanceMajor[0].CustomerMgr.ToString();
                    this.txtCustomerMgr.Value = instanceMajor[0].CustomerMgrName;
                    this.CustomerDirector.Value = instanceMajor[0].CustomerDirector.ToString();
                    this.txtCustomerDirector.Value = instanceMajor[0].CustomerDirectorName;
                    this.CustomerManager.Value = instanceMajor[0].CustomerManager.ToString();
                    this.txtCustomerManager.Value = instanceMajor[0].CustomerManagerName;

                    this.txtClassFrontServer.Value = instanceMajor[0].TrainName;
                    this.txtXm.Value = instanceMajor[0].UName;
                    this.txtStartDate.Value = instanceMajor[0].BeginDate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : instanceMajor[0].BeginDate.ToString("yyyy-MM-dd");
                    this.txtAddTime.Value = instanceMajor[0].StartDate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : instanceMajor[0].StartDate.ToString("yyyy-MM-dd");
                    this.txtPmExplain.Value = instanceMajor[0].Explain;
                    if ((instanceMajor[0].StartUser.ToString().Length > 0 && instanceMajor[0].StartUser != Guid.Empty) || strView == "check")
                    {
                        this.AddUser.Value = instanceMajor[0].StartUser.ToString(); //内训填写人ID
                        this.txtAddUser.Value = instanceMajor[0].AddPmName;//内训填写人
                    }
                    else
                    {
                        this.AddUser.Value = this.UserId.ToString();
                        this.txtAddUser.Value = this.User.UName;
                    }
                    this.txtAddTime.Value = instanceMajor[0].StartDate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : instanceMajor[0].StartDate.ToString("yyyy-MM-dd");
                    this.txtDay.Value = instanceMajor[0].DateSum.ToString() == "0" ? "" : instanceMajor[0].DateSum.ToString();
                    this.txtMemember.Value = instanceMajor[0].InnerTeacherName;//内训成员
                    this.txtEndWriteTime.Value = instanceMajor[0].TrainDate.ToString("yyyy-MM-dd") == "0001-01-01" ? strView == "check" ? "" : DateTime.Now.ToString("yyyy-MM-dd") : instanceMajor[0].TrainDate.ToString("yyyy-MM-dd"); //实施填写时间

                    this.txtEndTime.Value = instanceMajor[0].EndDate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : instanceMajor[0].EndDate.ToString("yyyy-MM-dd");
                    // this.txtExplain.Value = instanceMajor[0].SsExplain;
                    if ((instanceMajor[0].TrainUser.ToString().Length > 0 && instanceMajor[0].TrainUser != Guid.Empty) || strView == "check")
                    {
                        this.hdnEnd.Value = instanceMajor[0].TrainUser.ToString();
                        this.txtEndUser.Value = instanceMajor[0].SsName;
                    }
                    else
                    {
                        this.hdnEnd.Value = this.UserId.ToString();
                        this.txtEndUser.Value = this.User.UName;
                    }
                    txtOut.Value = instanceMajor[0].OutValue;
                    txtInner.Value = instanceMajor[0].InnerValue;
                    txtInnerGrade.Value = instanceMajor[0].exp3;
                    this.txtSatisfy.Value = instanceMajor[0].WellDegree.ToString() == "0" ? "" : instanceMajor[0].WellDegree.ToString();
                    this.txtMydTime.Value = instanceMajor[0].DegreeDate.ToString("yyyy-MM-dd") == "0001-01-01" ? strView=="check"?"": DateTime.Now.ToString("yyyy-MM-dd") : instanceMajor[0].DegreeDate.ToString("yyyy-MM-dd"); //市场填写时间
                    if ((instanceMajor[0].DegreeUser.ToString().Length > 0 && instanceMajor[0].DegreeUser != Guid.Empty) || strView == "check")
                    {
                        this.hdnMyd.Value = instanceMajor[0].DegreeUser.ToString();
                        this.txtMydUser.Value = instanceMajor[0].MarketName;
                    }
                    else
                    {
                        this.hdnMyd.Value = this.UserId.ToString();
                        this.txtMydUser.Value = this.User.UName;
                    }
                    //this.txtMarketExplain.Value = instanceMajor[0].Explain;

                    //this.Memember.Value = instanceMajor[0].PMemember;//项目成员ID
                    IList<V_CrmDeliverTeacher> listCusoterPay = null;
                    if (strDescript == "nxja")
                    {
                        listCusoterPay = bll.GetAllTeacherInfo(instanceMajor[0].ID, "nxja");
                        if (listCusoterPay != null && listCusoterPay.Count > 0)
                        {
                            string strTmp = listCusoterPay[0].Exp2;
                            if (strTmp.Contains(','))
                            {
                                string[] arrTtemp = listCusoterPay[0].Exp2.Split(',');
                                string[] arrTsemp = listCusoterPay[0].Exp1.Split(',');
                                listCusoterPay.Clear();
                                for (int i = 0; i < arrTtemp.Length; i++)
                                {
                                    V_CrmDeliverTeacher teacher = new V_CrmDeliverTeacher();
                                    teacher.Exp2 = arrTtemp[i];
                                    teacher.Exp1 = arrTsemp[i];
                                    listCusoterPay.Add(teacher);
                                }
                            }
                        }
                    }
                    else
                    {
                        listCusoterPay = bll.GetAllTeacherInfo(instanceMajor[0].ID, "");
                    }

                    if (listCusoterPay != null && listCusoterPay.Count > 0)
                    {
                        IList<BaseEntity> _listCusoterPay = new List<BaseEntity>();
                        foreach (var _e in listCusoterPay)
                        {
                            _listCusoterPay.Add(_e);
                        }
                        strTeacher = _listCusoterPay.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                    }
                    IList<BaseEntity> _listDeliverTrainInfo = new List<BaseEntity>();
                    foreach (var _e in instanceMajor)
                    {
                        _listDeliverTrainInfo.Add(_e);
                    }
                    strDeliverTrainInfo = _listDeliverTrainInfo.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                    //交付单状态选中
                    //DropDownListControl.DropDownListSelect(this.DliverState, instanceMajor[0].DliverState.ToString());
                }
                this.WorkTaskInsId.Value = this.GetStringFromQueryString("WorktaskInsId", "");
                this.WorkFlowInsId.Value = instanceMajor[0].WorkFlowInsId.ToString();
                wfid = instanceMajor[0].WorkFlowId.ToString();
                wtid = instanceMajor[0].WorkTaskId.ToString();

            }

        }
        /// <summary>
        /// 初始化流程数据
        /// </summary>
        /// <param name="workflowInsId"></param>
        public void Inits(string workflowInsId)
        {
            DataTable dt = client.GetWorkflowdetail(workflowInsId);
            //if (dt == null || dt.Rows.Count == 0)
            //{
            //    gvworkflowMessage.Visible = false;
            //}
            //else
            //{
            gvworkflowMessage.DataSource = dt;
            gvworkflowMessage.DataBind();
            //}
        }

        #region-----------
        public string PageTitle
        { get { return this.GetStringFromQueryString("title", "") == "" ? "交付申请单--内训" : this.GetStringFromQueryString("title", ""); } }
        public string strDescript
        { get { return this.GetStringFromQueryString("descript", ""); } }
        public string Teacher
        {
            get { return strTeacher; }
        }
        public string Flag
        { get { return this.GetStringFromQueryString("flag", ""); } }
        public string ListRecord
        { get { return GetListCrmProduct(); } }
        /// <summary>
        /// 弹出列表客户经理ID
        /// </summary>
        public string addUser { get { return this.UserId.ToString(); } }
        private string GetListCrmProduct()
        {
            IList<V_CrmOrderProduct> list = order.GetAllOrderProduct(new Guid(this.ID.Value));
            if (list != null && list.Count > 0)
            {
                return ZX.Common.Globals.ObjectToJSON(list);
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 信息集合
        /// </summary>
        public string StrDeliverTrainInfo
        { get { return this.strDeliverTrainInfo; } }
        public string strWorkFlowId
        { get { return this.GetStringFromQueryString("wfid", "") == "" ? wfid : this.GetStringFromQueryString("wfid", ""); } }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskId
        { get { return this.GetStringFromQueryString("wtid", "") == "" ? wtid : this.GetStringFromQueryString("wtid", ""); } }
        public string strOperatorInstanceId
        { get { return this.GetStringFromQueryString("OperatorInstanceId", ""); } }
        public string strWorkflowInsId
        { get { return this.GetStringFromQueryString("WorkflowInsId", ""); } }
        public string strView
        { get { return this.GetStringFromQueryString("view", ""); } }
        public string strOrderProduct
        {
            get { return orderProduct; }
        }

        public string strStartDate
        {
            get { return txtStartDate.Value; }
        }
        public string strMemember
        {
            get { return Memember.Value; }
        }
        public string strExplain
        {
            get { return txtPmExplain.Value; }
        }
        public string strOperStatus
        {
            get { return this.GetStringFromQueryString("OperStatus", ""); }
        }
        public string strEndDate
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd"); }
        }
        public string strOrderId
        {
            get { return orderId; }
        }
        public string strProductId
        {
            get { return productId; }
        }
        #endregion
    }
}